package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class DodajZagrozenieInputFrame extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3831002609736364207L;

	JLabel l;
	JTextArea text1, text2;
	JLabel label1, label2;
	JButton button1, button2;
	String n, i;
	Float d;

	public DodajZagrozenieInputFrame() throws HeadlessException {
		super("Dodaj zagrozenie");
	}
	
	public void start()
	
    {
		label1 = new JLabel("Nazwa: ");
	    label2 = new JLabel("Opis:     ");

	    text1 = new JTextArea(3,20);
	    text2 = new JTextArea(7,20);
	    button1 = new JButton("Dodaj");
	    button2 = new JButton("Zamknij");

	    this.add(label1, BorderLayout.WEST);
	    this.add(text1, BorderLayout.EAST);
	    this.add(label2, BorderLayout.WEST);
	    this.add(text2, BorderLayout.EAST);
	    this.add(button1);
	    this.add(button2);
	    
	    button1.addActionListener(this );
	    button2.addActionListener(this );
		
	    setLayout(new FlowLayout());
	    setSize(325,250);
	    setVisible(true);
	    Toolkit kit = Toolkit.getDefaultToolkit();
	    Dimension screenSize = kit.getScreenSize();
	    setLocation((screenSize.width-325)/2, (screenSize.height-250)/2);
	    setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "Zamknij"){
			this.dispose();
		}
		else if(e.getActionCommand() == "Dodaj"){
			try {
			     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			     Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ABSI", "Admin","Admin");
			     CallableStatement statement = conn.prepareCall("EXEC DodajZagrozenie ?, ?"); 
			     	n = text1.getText();
			        i = text2.getText();
			     
			     statement.setString(1,n);
			     System.out.println(n);
			     statement.setString(2,i);
			     statement.execute();

			     conn.close();
			  } catch (Exception ex) {
			     ex.printStackTrace();
			  }
		}
		text1.setText("");
		text2.setText("");
		
	}

}
